package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.DictionaryDao;
import org.lq.ht.entity.Dictionary;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;
/**
 * 字典接口实现类
 * @author 郑奥宇
 * @phone 15993025053
 *
 * @package org.lq.ht.dao.impl
 * @date 2020年8月20日下午10:02:43
 */
public class DictionaryDaoImpl implements DictionaryDao{
	/**
	 * 添加
	 */
	@Override
	public int save(Dictionary t) {
		int num = 0;
		String sql = "insert into dictionary(name,type_id,note) values(?,?,?)";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getName(),t.getType_id(),t.getNote());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 修改
	 */
	@Override
	public int update(Dictionary t) {
		int num = 0;
		String sql = "update dictionary set name = ?,type_id=?,note=? where no = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getName(),t.getType_id(),t.getNote(),t.getNo());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 删除
	 */
	@Override
	public int delete(int no) {
		int num = 0;
		String sql = "delete from dictionary where no = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
		
	}
	/**
	 * 查询全部
	 */
	@Override
	public List<Dictionary> findAll() {
		List<Dictionary> list = new ArrayList<>();
		String sql = "select * from dictionary";
		try {
			list = JDBCUtil.executeQuery(sql, new Instantiation<Dictionary>() {
				@Override
				public Dictionary instance(ResultSet rs) {
					Dictionary dic = new Dictionary();
					try {
						dic.setNo(rs.getInt("no"));
						dic.setName(rs.getString("name"));
						dic.setType_id(rs.getInt("type_id"));
						dic.setNote(rs.getString("note"));
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return dic;
				}
			});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 通过序号查询
	 */
	@Override
	public Dictionary getById(int no) {
		List<Dictionary> list = new ArrayList<>();
		String sql = "select * from dictionary where no = ?";
		try {
			list = JDBCUtil.executeQuery(sql, new Instantiation<Dictionary>() {
				@Override
				public Dictionary instance(ResultSet rs) {
					Dictionary dic = new Dictionary();
					try {
						dic.setNo(rs.getInt("no"));
						dic.setName(rs.getString("name"));
						dic.setType_id(rs.getInt("type_id"));
						dic.setNote(rs.getString("note"));
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return dic;
				}
			}, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}


//	@Override
//	public Dictionary instance(ResultSet rs) {
//		Dictionary dic = new Dictionary();
//		try {
//			dic.setNo(rs.getInt("no"));
//			dic.setName(rs.getString("name"));
//			dic.setType_id(rs.getInt("type_id"));
//			dic.setNote(rs.getString("note"));
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return dic;
//	}
	
	/**
	 * 查找同一type_id 下的所有
	 */
	public List<Dictionary> findById(int type_id) {
		List<Dictionary> list = new ArrayList<>();
		String sql = "select * from dictionary where type_id = ?";
		try {
			list = JDBCUtil.executeQuery(sql, new Instantiation<Dictionary>() {
				@Override
				public Dictionary instance(ResultSet rs) {
					Dictionary dic = new Dictionary();
					try {
						dic.setNo(rs.getInt("no"));
						dic.setName(rs.getString("name"));
						dic.setType_id(rs.getInt("type_id"));
						dic.setNote(rs.getString("note"));
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return dic;
				}
			}, type_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	@Override
	public List isType() {
		List list = null;
		String sql = "SELECT goods.type FROM dictionary,goods WHERE dictionary.no=goods.type";
		try {
			list = JDBCUtil.executeQuery(sql, new Instantiation<Integer>() {
				@Override
				public Integer instance(ResultSet rs) {
					int num = 0;
					try {
						num = rs.getInt("type");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return num;
				}
			});
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("商品类型集合:"+list);
		return list;
	}
	@Override
	public List isUnit() {
		List list = null;
		String sql = "SELECT goods.unit FROM dictionary,goods WHERE dictionary.no=goods.unit";
		try {
			list = JDBCUtil.executeQuery(sql, new Instantiation<Integer>() {
				@Override
				public Integer instance(ResultSet rs) {
					int num = 0;
					try {
						num = rs.getInt("unit");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return num;
				}
			});
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("商品单位集合:"+list);
		return list;
	}
	@Override
	public List isRoomType() {
		List list = null;
		String sql = "SELECT room.type FROM dictionary,room WHERE dictionary.no=room.type";
		try {
			list = JDBCUtil.executeQuery(sql, new Instantiation<Integer>() {
				@Override
				public Integer instance(ResultSet rs) {
					int num = 0;
					try {
						num = rs.getInt("type");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return num;
				}
			});
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("房间类型集合:"+list);
		return list;
	}
	

	@Override
	public List isVipType() {
		// TODO Auto-generated method stub
		List list = null;
		String sql = "select vip.type from vip,dictionary where vip.type=dictionary.no";
		try {
			list = JDBCUtil.executeQuery(sql, new Instantiation<Integer>() {
				@Override
				public Integer instance(ResultSet rs) {
					int num = 0;
					try {
						num = rs.getInt("type");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return num;
				}
			});
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}
